$(function(){
	showCondition(false);
	var param=drgUI.setAjaxData(tabId);
	$('#'+tabId+' #drgInfo').html(param.drgCode+"-"+param.drgDesc);
	$.post(contextPath + "/mrQualityAnalysis/getGroupDetail.dhc", param).done(function(res){
		var groupdata = res.data.groupList;
		var caseData = res.data.caseList;
		var paramsDataTables={lengthChange:false,ordering:false};
		var columns=[ 
						{ "data": "casetype","title":"病例类型",
							"render": function(obj, type, full){
								var caseType = "";
								switch(full.CASE_FLAG)
								{
								case 0:
									caseType = "全部病例";
									break;
								case 1:
									caseType = "常规病例";
									break;
								case 2:
									caseType = "一般离散病例(低)";
									break;
								case 3:
									caseType = "一般离散病例(高)";
									break;
								case 4:
									caseType = "高离散病例(低)";
									break;
								case 5:
									caseType = "高离散病例(高)";
									break;
								}
								return caseType;
							 }
						},
						{ "data": "interval","title":"费用区间",
							"render": function(obj, type, full){
								var interval = "";
								switch(full.CASE_FLAG)
								{
								case 0:
									interval = "(0,＋∞)";
									break;
								case 1:
									interval = "["+(param.as_avg_fee*0.8).toFixed(2)+","+(param.as_avg_fee*1.2).toFixed(2)+"]";
									break;
								case 2:
									interval = "("+(param.as_avg_fee*0.5).toFixed(2)+","+(param.as_avg_fee*0.8).toFixed(2)+")";
									break;
								case 3:
									interval = "("+(param.as_avg_fee*1.2).toFixed(2)+","+(param.as_avg_fee*1.5).toFixed(2)+")";
									break;
								case 4:
									interval = "(0,"+(param.as_avg_fee*0.5).toFixed(2)+"]";
									break;
								case 5:
									interval = "["+(param.as_avg_fee*1.5).toFixed(2)+",＋∞)";
									break;
								}
								return interval;
							 }
						},
						{ "data": "CASECNT","title":"病例数",
							"render": function(obj, type, full,meta){
								return "<a data-rowindex='"+meta.row+"'>"+full.CASECNT+"</a>";
							 }
						},
						{ "data": "per","title":"病例占比(%)",
							"render": function(obj, type, full){
								return (full.CASECNT/caseData.length*100).toFixed(2);
							 }
						},
						{ "data": "AVGFEE","title":"次均费用"},
						{ "data": "DAYFEE","title":"日均费用"},
						{ "data": "AVGDAY","title":"平均住院日"}
	                 ];
	    var table =$('#'+tabId+' #caseTypeTable').dataTableBasic(columns,groupdata,paramsDataTables);
	    $('#'+tabId+' #caseTypeTable tr a').unbind();
	    $('#'+tabId+' #caseTypeTable').on( 'click', 'tr a', function () {
	    	var index = $(this).attr("data-rowindex");
	    	var rowData = table.row( index ).data();
	    	caseList(rowData.CASE_FLAG,res.data);
	    	$('#'+tabId+' .nav-item[data-index="bllb1"]').click();//跳转到 病历列表
	    	
	    } );
	    $('#'+tabId+' #caseTypeTable tbody tr:eq(0) a').click();//默认加载全部
	})
});
function caseList(type,data){
	//绘制饼图
	var chartData = data[type];
	var paramsDep={lengthChange:false,ordering:false,pageLength:5};
	var depcolumns=[ 
					{ "data": "DEP_NAME","title":"科室名称"},
					{ "data": "CNT","title":"病例数"}
              ];
	var depCallback = function( oSettings ) {
		var pieData = [];//手术级别
		var start = oSettings._iDisplayStart+oSettings._iDisplayLength;
		var end = oSettings._iDisplayStart;
		for(var i=start-1;i>=end;i--){
			var row = oSettings.aoData[i];
			if(typeof(row) != "undefined")
			{
				rowData = row._aData;
				pieData.push({name:rowData.DEP_NAME,value:rowData.CNT});
			}
		}
		pieCharts($("#"+tabId+" #depDistributeChart"), pieData, '科室分布', '');
	}
	paramsDep.drawCallback=depCallback;
	$('#'+tabId+' #depDistributeTable').dataTableBasic(depcolumns,chartData,paramsDep);
	var paramsDataTables={scrollX:true,lengthChange:false,columnDefs:[{"targets": [ 13 ],"visible": false}],order:[[ 13, "desc" ]]};
	var caseData = data.caseList;
	if(type>0)
	{
		caseData=caseData.filter(function(obj){
			return obj.CASE_FLAG == type;
		}); 
	}
	var caseType = "";
	switch(type)
	{
	case 0:
		caseType = "全部病例";
		break;
	case 1:
		caseType = "常规病例";
		break;
	case 2:
		caseType = "一般离散病例(低)";
		break;
	case 3:
		caseType = "一般离散病例(高)";
		break;
	case 4:
		caseType = "高离散病例(低)";
		break;
	case 5:
		caseType = "高离散病例(高)";
		break;
	}
	$('#'+tabId+' #caseType').html(caseType);
	var columns=[ 
					{ "data": "MR_NO","title":"病案号",
						"render": function(obj, type, full){
		 					return "<a href=\"javascript:medicalRecord('"+full.PMAININDEX+"','"+full.PNAME+"')\">"+full.MR_NO+"</a>";
						 }
					},
					{ "data": "PNAME","title":"姓名"},
					{ "data": "AGE_YEAR","title":"年龄",
						"render": function(obj, type, full){
							var result = "";
							if(full.AGE_YEAR !=null)
							{
								result += full.AGE_YEAR+"岁";
							}
							if(full.AGE_MONTH !=null)
							{
								result += full.AGE_MONTH+"月";
							}
							if(full.AGE_DAY !=null)
							{
								result += full.AGE_DAY+"天";
							}
							return result;
						 }
					},
					{ "data": "OUT_DATE","title":"出院日期"},
					{ "data": "DEP_NAME","title":"出院科室"},
					{ "data": "COSTAMOUNT","title":"次均费用"},
					{ "data": "AS_AVG_FEE","title":"标杆费用"},
					{ "data": "FEEGAP","title":"与费用标杆差距"},
					{ "data": "IN_DAYS","title":"平均住院日"},
					{ "data": "AS_AVG_DAYS","title":"标杆住院日"},
					{ "data": "DAYGAP","title":"与住院日标杆差距"},
					{ "data": "DIAG","title":"主要诊断",
						"render": function(obj, type, full){
							return full.DIAG_CODE+"-"+full.DIAG_DESC;
						 }
					},
					{ "data": "OPER","title":"主要手术或操作",
						"render": function(obj, type, full){
							if(full.OPER_CODE&&full.OPER_CODE)
							{
								return full.OPER_CODE+"-"+full.OPER_DESC;
							}
						 }
					},
					{ "data": "hidecol","title":"隐藏列",
						"render": function(obj, type, full){
							return Math.abs(full.FEEGAP);
						 }
					}
              ];
	$('#'+tabId+' #caseListTable').dataTableBasic(columns,caseData,paramsDataTables);
}


